#include <iostream>
#include <string>
#include <mutex>
#include <thread>
#include <future>
#include <boost/dynamic_bitset.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/assign.hpp>
#include <boost/multi_array.hpp>
#include <cmath>
#include <functional>
#include <cstdint>
#include <limits>
#include <boost/bimap/bimap.hpp>


using namespace std;
/*
select
update
insert
delete
db:
item:itemlen:property:propertylen[1,2]:data:property:propertylen[1,2]:data
pa1
[ids]data
pb2
[ids]data
pc3
pd4


type?
isindex?
isunique?
name
id:pos:len:id:pos:len:id:pos:len:id:pos:len
id:data0:id:data0:id:data0
A-Zname
id:data0:id:data0
repeat- id:id:id:id:id:id:data


ID:p1,p2,p3,p4,p5:ID:p1,p3,p6
freespace segment
space discript + capacity + sizeof(pos)+max(len)
4[type][4byte]

be class in
ID:p1,p2,p3,p4,p5:ID:p1,p3,p6

ext:
xnsche
.sche
xndata
.data
xnposi
.posi
xnspac
.spac
xnorde
.orde

xnprop
.prop


\0xxxxdatadatadatadata\03453\0


*/

/*
descript:
item:pos:len
item:pos:len
item:pos:len
item:pos:len
Gap:
pos:len
pos:len

*/

class xnDb
{
    open();//path
};

class xnPropSet
{
    open();//path+filename
};

//nfile op
//file type 1


class xnInitData
{
public:
    xnInitData()
    {

    }
    ~xnInitData()
    {

    }
public:

private:

};

class xnOptSpace
{
public:
    xnOptSpace() {}
    ~xnOptSpace() {}
public:

protected:
private:
};

typedef bimap<uint32_t,uint8_t>    FixBiMap;

class xnFixSpaceManeger
{
public:
    uint32_t addfreespace(uint32_t pos,uint8_t len)
    {
        m_bimap.insert(FixBiMap::value_type(pos,len))
    }
    uint32_t getSpace(int len)
    {
        if(m_bimap.empty())
        {
            uint32_t retdatapos=m_nextdatapos;
            m_nextdatapos+=len;
            return retdatapos;
        }else{
            auto it=m_bimap.rfind(len);
            if(it!=m_bimap.end())
            {
                uint32_t pos=it->first();
                m_bimap.erase(it);
                return pos
            }
        }
        uint32_t retdatapos=m_nextdatapos;
        m_nextdatapos+=len;
        return retdatapos;
    }
protected:
private:
    FixBiMap m_bimap;
}

class xnSpaceManagerI4
{
public:
    xnSpaceManagerI4(ofstream *spacfile)
    {
        m_spacfile=spacfile;
    }
    ~xnSpaceManagerI4()
    {

    }
public:
    uint32_t addfreespace(uint32_t pos,uint8_t len)
    {
        if(m_idata_end+sizeof(uint32_t)+sizeof(uint8_t)<m_idata_max)
        {
            m_spacfile->seekg(m_idata_end,iso_base::beg);
            m_spacfile->write();
            m_spacfile->write();
        }else{
            m_spacfile->seekg(0,iso_base::end);
            char dd[10];
            uint32_t u32max=numeric_limits<uint32_t>::max();
            memcpy(dd,&u32max,sizeof(uint32_t))
            uint32_t u8max=numeric_limits<uint8_t>::max();
            memcpy(dd,&u8max,sizeof(uint8_t))
            for(int i=0;i<1000000)
            {
                m_spacfile->write(dd,sizeof(uint32_t)+sizeof(uint8_t));
            }
        }
    }
    uint32_t getSpace(int len)
    {
        try
        {
            m_spacfile->seekg(200,iso_base::beg);
        if(m_spacfile->good())
        {
            char *spacdata=NULL;
            while(!m_spacfile->eof())
            {
                m_spacfile->read(spacedata,readblocksize);
                int gcount=m_spacfile->gcount();
                if(gcount>4)
                {
                    uint32_t spacedata_pos=0;
                    do
                    {
                        if(reinterpret_cast<uint32_t>(spacedata+spacedata_pos)!=numeric_limits<uint32_t>::max())
                        {
                            if(reinterpret_cast<uint8_t>(spacedata+spacedata_pos+sizeof(uint32_t))==len)
                            {
                                return reinterpret_cast<uint32_t>(spacedata+spacedata_pos);
                            }
                        }else{
                            throw __LINE__;
                        }
                        spacedata_pos+=sizeof(uint32_t)+sizeof(uint8_t);
                    }while(spacedata_pos<gcount)
                }else{
                    throw __LINE__;
                }
            }
        }
        }catch(int errocode){
            uint32_t retdatapos=m_nextdatapos;
            m_nextdatapos=m_nextdatapos+len;
            return retdatapos;
        }
        uint32_t retdatapos=m_nextdatapos;
        m_nextdatapos=m_nextdatapos+len;
        return retdatapos;
    }
}
protected:
private:
const static int readblocksize=20000000;
ofstream *m_spacfile;
};

class xnFreeSpaceManager
{
    friend xnOptSpace;
public:
    xnFreeSpaceManager(char *spacefile)//.spac
    {

    }
    ~xnFreeSpaceManager()
    {

    }
public:
    deque<std::pair<int,int> > getSpace(int &len)
    {

    }
protected:
private:

};

class xnDataWrite
{
public:
    xnDataWrite() {}
    ~xnDataWrite() {}
public:

private:

};

int main()
{


    return 0;
}
